<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// +----------------------------------------------------------------------
// | 后台管理系统用户权限管理
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// | 2015-7-30下午4:27:30
// +----------------------------------------------------------------------

class Authmanager extends MY_Controller{
	
	private $action_method;
	public function __construct(){
		parent::__construct();
		$this->action_method = array('delete', 'disable');
	}
	
	/**
	 * 角色管理首页
	 * @author Baip
	 */
	public function index(){
		
		$where = array('module'=>'admin');
		$this->load->model('Authgroup_model', 'auth');//载入角色模型类
		$record_count = $this->auth->getCount($where);//获取总数
		$list = $this->auth->getData($where);
		
		
		$this->load->vars('list', $list);
		$this->load->vars('meta_title', '权限管理');
		$this->load->vars('record_count', $record_count);
		$this->load->view('authmanager/index');
	}
	
	/**
	 * 新增角色
	 * @author Baip
	 */
	public function create(){
		//引入表单验证类
		$this->load->library('form_validation');
		//验证表单及是post提交的时候才可以入库
		if( $this->form_validation->run('authmanager') && IS_POST){
			$this->load->model('Authgroup_model', 'auth');//载入角色模型类
			$insert_id = $this->auth->addData();
			if ( $insert_id ){
				$this->show_message('success','添加成功');
			}else{
				$this->show_message('error','添加失败');
			}
		}else{
			$this->load->vars('meta_title', '创建角色');
			$this->load->view('authmanager/create');
		}
	}
	
	/**
	 * 修改角色
	 * @param unknown_type $id
	 */
	public function modfiy(){
		$get_id =  IS_POST ? intval( $this->input->post('id') ) : intval( $this->uri->segment(3) );
		if ( 0 === $get_id ) {
			$this->show_message('error', '参数错误', site_url('authmanager/index'));
		}
		//引入表单验证类
		$this->load->library('form_validation');
		$this->load->model('Authgroup_model', 'auth');//载入角色模型类
		//验证表单及是post提交的时候才可以入库
		if( $this->form_validation->run('authmanager') && IS_POST){
			$id = intval( $this->input->post('title',id) );//修改的id
			$update_status = $this->auth->updateData( array('id'=> $get_id) );
			if( $update_status ){
				$this->show_message('success','操作成功', site_url('authmanager/index'));
			}else{
				$this->show_message('error','操作失败', site_url('authmanager/modfiy/'.$get_id));
			}
			
		}else{
			
			$info = $this->auth->getFind(1, array('id'=>$get_id) );
			$this->load->vars('meta_title', '修改角色');
			$this->load->vars('info', $info);//修改详情数据
			$this->load->view('authmanager/create');
		}	
	}
	
	/**
	 * 操作状态(对角色的删除，启用，禁用等操作)
	 * @author Baip
	 */
	public function changesstatus(){
		//操作方式（delete,disable）
		$method = $this->uri->segment(3);
		if ( !in_array( $method , $this->action_method) ){
			ajaxreturn(0, '操作方式错误', site_url('authmanager/index'));
		}
		//如果为post提交要接收id数组，否则get提交时接收第4个参数的值
		if ( IS_POST ){
			$id = $this->input->post('id');
			if ( count( $id ) < 1 ){
				ajaxreturn(0, '参数错误', site_url('authmanager/index'));
			}
		}else{
			$id = intval( $this->uri->segment(4) );
			if ( 0 ===  $id ){
				ajaxreturn(0, '参数错误', site_url('authmanager/index'));
			}
		}
		$this->load->model('Authgroup_model', 'auth');//载入角色模型类
		switch ($method){
			//删除数据
			case 'delete':
				$status = $this->auth->deleteData($id,2);
			break;
			//禁用或启用
			case 'disable':
			break;
		}
		
		if ( $status ){
			ajaxreturn(1, '操作成功', site_url('authmanager/index'));
		}else{
			ajaxreturn(0, '操作失败', site_url('authmanager/index'));
		}
		
	}
	
	/**
	 * 访问授权
	 */
	public function access(){
		
	}
	
	/**
	 * 用户组授权用户列表
	 */
	public function user($group_id){
		if(empty($group_id)){
			$this->show_message('0', '参数失败');
		}
		//引入用户组模型类
		require_once FCPATH .'/admanager/models/Authgroup_model.php';
		$auth_group_class = new Authgroup_model();
		$auth_group = $auth_group_class->getField( array('status >='=>0,'module'=>'admin', 'type'=>$auth_group_class::TYPE_ADMIN) );
		var_dump( $auth_group );exit;
		
		$auth_group = M('AuthGroup')->where( array('status'=>array('egt','0'),'module'=>'admin','type'=>AuthGroupModel::TYPE_ADMIN) )
		->getfield('id,id,title,rules');
		$prefix   = C('DB_PREFIX');
		$l_table  = $prefix.(AuthGroupModel::MEMBER);
		$r_table  = $prefix.(AuthGroupModel::AUTH_GROUP_ACCESS);
		$model    = M()->table( $l_table.' m' )->join ( $r_table.' a ON m.uid=a.uid' );
		$_REQUEST = array();
		$list = $this->lists($model,array('a.group_id'=>$group_id,'m.status'=>array('egt',0)),'m.uid asc',null,'m.uid,m.nickname,m.last_login_time,m.last_login_ip,m.status');
		int_to_string($list);
		$this->assign( '_list',     $list );
		$this->assign('auth_group', $auth_group);
		$this->assign('this_group', $auth_group[(int)$_GET['group_id']]);
		$this->meta_title = '成员授权';
		$this->display();
	}
}